package val.to.common;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AsyncTasksQueue{
	
	
    final int maxPoolSize = 10;
    private ExecutorService mExecutor = Executors.newFixedThreadPool(maxPoolSize);
	
	public void ClearAllPendingTasks()
	{
		//synchronized(waitingTasks)
		//{
		//	waitingTasks.clear();
		//}
		
		//TODO
	}
	
	public void AddTask(Runnable newTask)
	{
		/*
		synchronized(waitingTasks)
		{
			waitingTasks.add(newTask);
			waitingTasks.a
		}
		*/
		mExecutor.submit(newTask);
		}
	
	/*
	public void run()
	{
		while (true)
		{
			List<AsyncTask<Void,Void,Void>> workingTasks = new ArrayList<AsyncTask<Void,Void,Void>>(); 
			
			for (int i = 0; i < MAX_WORKERS; i++)
			{
				AsyncTask<Void,Void,Void> task = null; 
				
				synchronized(waitingTasks)
				{
					if (waitingTasks.size() > 0)
					{
						task = waitingTasks.get(0);
					}
				}
				
				if (task != null)
				{
					try
					{
						task.execute();
					}
					catch(Exception re)
					{
						continue;
					}
					
					synchronized(waitingTasks)
					{
						waitingTasks.remove(0);
					}
					
					workingTasks.add(task);
				}
			}
			
			while (workingTasks.size() > 0)
			{
				for (int i = 0; i < workingTasks.size(); i++)
				{
					if (workingTasks.get(i).getStatus() != AsyncTask.Status.RUNNING)
					{
						workingTasks.remove(workingTasks.get(i));
						i--;
					}
				}
				
				try
				{
					Thread.sleep(50);
				}
				catch (InterruptedException ex)
				{
					
				}
			}
			
			try
			{
				Thread.sleep(1000);
			}
			catch (InterruptedException ex)
			{
				
			}
		}
	}
	*/
}
